(function ($) {
    $.alterAuth = function () {
        return {
            schoolList: parent.$.dataCenter.schoolList,
            resourceid: "",
            auth:1,
            init: function () {
                this.listener();
                this.initLayui();
                this.alreadySchool();
            },
            /**
             * 添加各按钮所需要的监听事件
             */
            listener: function () {
                $("#searchInput").keydown(function () {
                    if (event.keyCode == 13) {
                        $.alterAuth.searchSchool();
                    }
                });
                $("#searchBtn").click(function () {
                    $.alterAuth.searchSchool();
                });
                /**
                 * 删除已经绑定的学校
                 * **/
                $(".alreadySchool").on("click", ".delete", function () {
                    var othis=$(this);
                    layer = (parent.layer === undefined) ? layui.layer : parent.layer;
                    layer.confirm('是否删除', {
                        btn: ['确定', '取消'] //按钮
                    }, function (index, layero) {
                        othis.parent("li").remove();
                        $.alterAuth.schoolList=[];
                        $.each($(".alreadySchool li"), function (i, liDom) {
                            console.log($(liDom))
                            var obj={};
                            obj.schoolId=$(liDom).attr("data-id");
                            obj.schoolName=$(liDom).find(".schoolName").html();
                            $.alterAuth.schoolList.push(obj);
                        });
                        $.alterAuth.alter();
                        layer.close(index);
                    }, function () {
                    });
                });
            },

            initLayui: function () {
                layui.use(['layer', 'form', 'element'], function () {
                    var $ = layui.jquery, layer = (parent.layer === undefined) ? layui.layer : parent.layer,
                        form = layui.form, element = layui.element;
                    form.render('checkbox');
                    //监听选择公开还是指定学校
                    form.on('checkbox(auth)', function(data){
                        $("#searchInput").val("");
                        if($(data.elem).attr("data-auth")=="6"){//如果是指定
                            //引用完成后input全置为空
                            $.each($(".public input[type='checkbox']"), function (i, inputs) {
                                $(inputs).attr("checked", false);
                            });
                        }else{
                            $(".person input[type='checkbox']").attr("checked", false);
                        }
                        form.render('checkbox');
                        var arr=$("input.authSel[type='checkbox']:checked");
                        //设置auth
                        if(arr.length=="2"){
                            $.alterAuth.auth="5";
                        }else if(arr.length=="1"){
                            $.alterAuth.auth=$("input.authSel[type='checkbox']:checked").attr("data-auth");
                        }else{
                            $.alterAuth.auth="1";
                        }
                        if($.alterAuth.auth=="6"){
                            $(".alreadySchool").css("display","block");
                            $(".newSchool").css("display","none");
                        }else{
                            $(".newSchool,.alreadySchool").css("display","none");
                        }
                    });
                });
            },
            //查询学校
            searchSchool:function(){
                // 如果选中指定学校 则进行查询
                if($(".person input[type='checkbox']").attr("checked")!=undefined){
                    var searchSchool = {
                        "content": $("#searchInput").val(),
                        "resourceid": $.alterAuth.resourceid
                    };
                    updateObject(Doman.dataCenterUrl + Doman.searchSchool, false, JSON.stringify(searchSchool), function (result) {
                        if (result.code == 200) {
                            if(result.schoolDTOList.length>0){
                                $(".alreadySchool").css("display","none");
                                $(".newSchool").css("display","block");
                                $.alterAuth.searchSchoolList(result.schoolDTOList);
                            }
                        } else {
                            layer.msg(result.message, {icon: 2});
                        }
                    }, function (result) {
                        layer.msg(JSON.parse(result.responseText).message, {icon: 2});
                    });
                }
            },
            //搜索列表
            searchSchoolList:function(schoolList){
                var html="";
                $.each(schoolList, function (i, data) {
                    html += ' <li data-schoolId=' + data.schoolId + '><input type="checkbox" name="" title='+data.schoolName+' lay-skin="primary"></li>';
                });
                $(".newSchool").html(html);
                $.alterAuth.initLayui();
            },
            //添加学校列表
            addSchool:function(){
                var inputs = $(".newSchool").find("input[type='checkbox']:checked");
                if (inputs.length > 0) {
                    $.each(inputs, function (i, inputDom) {
                        var obj={};
                        obj.schoolName=$(inputDom).attr("title");
                        obj.schoolId=$(inputDom).parent().attr("data-schoolId");
                        $.alterAuth.schoolList.push(obj);
                    });
                    $.alterAuth.alreadySchool();
                    $.alterAuth.alter();
                }
                $(".newSchool").css("display","none");
                $(".alreadySchool").css("display","block");
                $("#searchInput").val("");
            },
            alreadySchool: function () {
                //如果是单独资料 搜索则删除重复
                if ($.alterAuth.schoolList .length == 1) {
                    $.alterAuth.resourceid=parent.$.dataCenter.alterAuthIdList[0];
                }
                if ($.alterAuth.schoolList.length > 0) {
                    var html = "";
                    $.each($.alterAuth.schoolList, function (i, data) {
                        html += '<li data-schoolId=' + data.schoolId + '><span class="schoolName">' + data.schoolName + '</span><span class="icon delete"></span></li>'
                    });
                    $(".alreadySchool").html(html);
                }
            },
            alter: function () {
                var schoolList=[];
                if($.alterAuth.auth=="6"){
                    schoolList=$.alterAuth.schoolList
                }
                var alterAuth = {
                    "auth": $.alterAuth.auth,
                    "idList": parent.$.dataCenter.alterAuthIdList,
                    "schoolList": schoolList,
                    "token":User.token
                };
                updateObject(Doman.dataCenterUrl + Doman.alterAuth, false, JSON.stringify(alterAuth), function (result) {
                    if (result.code == 200) {
                        layer.msg(result.message, {icon: 1});
                    } else {
                        layer.msg(result.message, {icon: 2});
                    }
                }, function (result) {
                    layer.msg(JSON.parse(result.responseText).message, {icon: 2});
                });
            }
        }
    }();
    $.alterAuth.init();
})(jQuery);